package com.atjinggu.jinggumail.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.atjinggu.jinggumail.common.to.es.SkuEsModule;
import com.atjinggu.jinggumail.search.config.EsClientConfig;
import com.atjinggu.jinggumail.search.constant.EsConstant;
import com.atjinggu.jinggumail.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author jingming
 * @Type ProductSaveServiceImpl.java
 * @Desc
 * @date 2021/1/8 12:14
 */
@Service
@Slf4j
public class ProductSaveServiceImpl implements ProductSaveService {

    @Autowired
    RestHighLevelClient client;

    /**
     * PUT /product
     * {
     * 	"mappings": {
     * 		"properties": {
     * 			"skuId": {
     * 				"type": "long"
     *                        },
     * 			"spuId": {
     * 				"type": "keyword"
     *            },
     * 			"skuTitle": {
     * 				"type": "text",
     * 				"analyzer": "ik_smart"
     *            },
     * 			"skuPrice": {
     * 				"type": "keyword"
     *            },
     * 			"skuImg": {
     * 				"type": "keyword",
     * 				"index":false,
     * 				"doc_values":false
     *            },
     * 			"saleCount": {
     * 				"type": "long"
     *            },
     * 			"hasStock": {
     * 				"type": "boolean"
     *            },
     * 			"hotScore": {
     * 				"type": "long"
     *            },
     * 			"brandId": {
     * 				"type": "long"
     *            },
     * 			"catalogId": {
     * 				"type": "long"
     *            },
     * 			"brandName": {
     * 				"type": "keyword",
     * 				"index": false,
     * 				"doc_values": false
     *            },
     * 			"brandImg": {
     * 				"type": "keyword",
     * 				"index": false,
     * 				"doc_values": false
     *            },
     * 			"catalogName": {
     * 				"type": "keyword",
     * 				"index": false,
     * 				"doc_values": false
     *            },
     * 			"attrs": {
     * 				"type": "nested",
     * 				"properties": {
     * 					"attrId": {
     * 						"type": "long"
     *                    },
     * 					"attrName": {
     * 						"type": "keyword",
     * 						"index": false,
     * 						"doc_values": false
     *                    },
     * 					"attrValue": {
     * 						"type": "keyword"
     *                    }
     *
     *                }
     *            }* 		}
     *
     *    }
     * }
     * @param esModuleList
     * @return
     * @throws IOException
     */
    @Override
    public boolean productStatusUp(List<SkuEsModule> esModuleList) throws IOException {
        //1.给es中建立索引 product.。在kibana中建立好映射关系
        BulkRequest bulkRequest = new BulkRequest();
        //2.给es中批量保存数据
        esModuleList.stream().forEach(item -> {
            IndexRequest indexRequest = new IndexRequest(EsConstant.PRPDUCT_INDEX);
            indexRequest.id(item.getSkuId().toString());
            indexRequest.source(JSON.toJSONString(item), XContentType.JSON);
            bulkRequest.add(indexRequest);
        });
        BulkResponse bulk = client.bulk(bulkRequest, EsClientConfig.COMMON_OPTIONS);
        boolean b = bulk.hasFailures();
        if(b){
            log.error("商品上架错误： {}",bulk.buildFailureMessage());
        }

        return b;


    }
}